package com.david.SendFileToGateway.file;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 自定义MultipartFile的实现
 */
public class MyMultipartFile implements MultipartFile {

    private final String name;

    private String orignalFileName;

    @Nullable
    private String contentType;

    private final byte[] content;

    public MyMultipartFile(String name,@Nullable byte[] content) {
        this(name,"",null,content);
    }


    public MyMultipartFile(String name,InputStream contentStream) throws IOException {
        this(name,"",null,FileCopyUtils.copyToByteArray(contentStream));
    }

    public MyMultipartFile(String name,@Nullable String orignalFileName,@Nullable String contentType,InputStream contentStream) throws IOException {
        this(name,orignalFileName,contentType, FileCopyUtils.copyToByteArray(contentStream));
    }

    public MyMultipartFile(String name,@Nullable String orignalFileName,@Nullable String contentType,@Nullable byte[] content){
        Assert.hasLength(name,"Name must not be null");
        this.name=name;
        this.orignalFileName=(orignalFileName!=null? orignalFileName:"");
        this.contentType=contentType;
        this.content=(content!=null?content:new byte[0]);
    }


    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getOriginalFilename() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public long getSize() {
        return 0;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return new byte[0];
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return null;
    }

    @Override
    public void transferTo(File file) throws IOException, IllegalStateException {

    }
}
